﻿using System.IO;
using UtilityToolsCollectInStandard.SerializeUtilityTools;

namespace UtilityToolsCollect.ObjectsLibrary.Configs
{
    public abstract class JsonConfigurationAbstract<T> : ConfigurationAbstract<T>, IConfiguration<T> where T : ConfigurationBase, new()
    {
        public static T Current
        {
            get
            {
                if (m_Current == DefaultEmpty)
                    Load();
                return m_Current!;
            }
            set
            {
                if (value == m_Current)
                    return;
                m_Current = value;
            }
        }

        public static void Load() { if (File.Exists(ConfigurationFilePath)) m_Current = SystemTextJson.DeserializeFormFile<T>(ConfigurationFilePath); m_Current?.Sort(); }
        public static void Save() { m_Current?.Sort(); SystemTextJson.SerializeToFile(ConfigurationFilePath, Current, SystemTextJson.DefaultSerializerOptions); }

    }
}
